package com.ihisomp.controller;

import com.ihisomp.base.Result;
import com.ihisomp.dto.SysDictionaryDto;
import com.ihisomp.service.ISysDictionaryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 系统公用字典 前端控制器
 * </p>
 *
 * @author zhangyong
 * @since 2021-12-21
 */
@RestController
@Api(value = "系统字典信息", tags = "系统字典信息")
@RequestMapping("/sysDictionary")
public class SysDictionaryController {

    Logger logger = LoggerFactory.getLogger(getClass());

    @Autowired
    private ISysDictionaryService sysDictionaryService;

    /**
     * 根据分类查询数据列表
     *
     * @param parentDictionaryKey
     * @return
     */
    @GetMapping("/selectList/{parentDictionaryKey}")
    @ApiOperation("根据分类查询数据列表")
    @ApiImplicitParam(name = "parentDictionaryKey", value = "字典分类主键", required = true)
    public Result selectList(@PathVariable("parentDictionaryKey") String parentDictionaryKey) {
        logger.info("数据列表：parentDictionaryKey={}", parentDictionaryKey);
        Result result = sysDictionaryService.selectList(parentDictionaryKey);
        return result;
    }

    /**
     * 查询数据元名称列表
     *
     * @return
     */
    @GetMapping("/selectParentDataList")
    @ApiOperation("数据元名称数据")
    public Result selectParentDataList() {
        logger.info("数据元名称列表");
        Result result = sysDictionaryService.selectParentDataList();
        return result;
    }

    /**
     * 新增数据
     *
     * @param sysDictionaryDto
     * @return
     */
    @PostMapping("/add")
    @ApiOperation("新增数据")
    @ApiImplicitParam(name = "sysDictionaryDto", value = "系统字典数据类", required = true)
    public Result add(@RequestBody SysDictionaryDto sysDictionaryDto) {
        logger.info("新增数据：sysDictionaryDto={}", sysDictionaryDto);
        Result result = sysDictionaryService.add(sysDictionaryDto);
        return result;
    }

    /**
     * 删除数据
     *
     * @param dictionaryKey
     * @return
     */
    @GetMapping("/delete/{dictionaryKey}")
    @ApiOperation("删除数据")
    @ApiImplicitParam(name = "dictionaryKey", value = "系统字典主键", required = true)
    public Result delete(@PathVariable("dictionaryKey") String dictionaryKey) {
        logger.info("删除数据：dictionaryKey={}", dictionaryKey);
        Result result = sysDictionaryService.delete(dictionaryKey);
        return result;
    }

    /**
     * 更新数据
     *
     * @param dictionaryKey
     * @param sysDictionaryDto
     * @return
     */
    @PostMapping("/update/{dictionaryKey}")
    @ApiOperation("更新数据")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "dictionaryKey", value = "系统字典主键", required = true),
            @ApiImplicitParam(name = "sysDictionaryDto", value = "系统字典数据类", required = true)})
    public Result update(@PathVariable("dictionaryKey") String dictionaryKey, @RequestBody SysDictionaryDto sysDictionaryDto) {
        logger.info("更新数据：dictionaryKey={}， sysDictionaryDto={}", dictionaryKey, sysDictionaryDto);
        Result result = sysDictionaryService.update(dictionaryKey, sysDictionaryDto);
        return result;
    }

    /**
     * 查询数据
     *
     * @param dictionaryKey
     * @return
     */
    @GetMapping("/get/{dictionaryKey}")
    @ApiOperation("查询数据")
    @ApiImplicitParam(name = "dictionaryKey", value = "系统字典主键", required = true)
    public Result get(@PathVariable("dictionaryKey") String dictionaryKey) {
        logger.info("查询数据：dictionaryKey={}", dictionaryKey);
        Result result = sysDictionaryService.get(dictionaryKey);
        return result;
    }

    /**
     * 分页查询
     *
     * @param page
     * @param size
     * @param sysDictionaryDto
     * @return
     */
    @PostMapping("/search/{page}/{size}")
    @ApiOperation("分页查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "当前页码", required = true),
            @ApiImplicitParam(name = "size", value = "每页条数", required = true),
            @ApiImplicitParam(name = "sysDictionaryDto", value = "系统字典数据类", required = true)})
    public Result search(@PathVariable("page") long page,
                         @PathVariable("size") long size,
                         @RequestBody(required = false) SysDictionaryDto sysDictionaryDto) {
        logger.info("分页查询：page={}， size={}, sysDictionaryDto={}", page, page, sysDictionaryDto);
        Result result = sysDictionaryService.search(page, size, sysDictionaryDto);
        return result;
    }

}
